import {BOARD} from "../configs/config";

class BoardUtils {

    /**
     * 模拟落子
     * @param requestGameMap
     * @param to
     */
    tryMoveChess = (requestGameMap: any, to: any): any => {
        // 避免原数据被改动，copy一份数据
        try {
            const gameMap = JSON.parse(JSON.stringify(requestGameMap));
            gameMap.push(to);
            return JSON.stringify(gameMap);
        } catch (e) {
            return requestGameMap;
        }
    }

    /**
     * 一维数组转二维
     * @param gameMap
     * @returns {[]}
     */
    listToArray = (gameMap: any) => {
        const result: any[] = [];
        // 坐标初始化
        for (let i = 0; i < BOARD.ROW_SIZE; ++i) {
            result[i] = [];
            for (let j = 0; j < BOARD.COL_SIZE; ++j) {
                result[i][j] = null;
            }
        }
        // 遍历所有可用节点
        gameMap.map((chess: any) => result[chess.x][chess.y] = chess);
        return result;
    }

    /**
     * 秒转成字符串分钟(如：60 -> 01:00)
     * @param seconds
     * @return {string}
     */
    secondsToMinuteStr = (seconds: number) => {
        if (!seconds || seconds <= 0) {
            return '00:00';
        } else {
            const minute = Math.floor(seconds / 60);
            const overSeconds = seconds % 60;
            const minuteStr = minute < 10 ? `0${minute}` : minute;
            const secondsStr = overSeconds < 10 ? `0${overSeconds}` : overSeconds;
            return `${minuteStr}:${secondsStr}`;
        }
    };
}

export default BoardUtils;